package com.shi.yibanjia.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shi.yibanjia.annotation.Auth;
import com.shi.yibanjia.annotation.MyLog;
import com.shi.yibanjia.common.Result;
import com.shi.yibanjia.entity.po.Publicity;
import com.shi.yibanjia.enums.AuthCodeEnum;
import com.shi.yibanjia.enums.LogType;
import com.shi.yibanjia.service.PublicityService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/publicity")
public class PublicityController {

    @Resource
    private PublicityService publicityService;

    @Auth(hasPerm = AuthCodeEnum.BIZ_PUBLICITY_DQL)
    @GetMapping("/{page}/{size}")
    @Cacheable(value = "yibanjia:publicity", key = "'page:'+#page+':size:'+#size")
    public Result page(@PathVariable("page") Integer page,
                       @PathVariable("size") Integer size) {
        IPage<Publicity> publicityIPage = publicityService.getPublicityPage(page, size);
        return Result.ok(publicityIPage);
    }

    @MyLog(type = LogType.DELETE)
    @Auth(hasPerm = AuthCodeEnum.BIZ_PUBLICITY_DML)
    @DeleteMapping("/{id}")
    @CacheEvict(value = "yibanjia:publicity", allEntries = true)
    public Result removeById(@PathVariable("id") Long id) {
        publicityService.removeById(id);
        return Result.ok();
    }

    @MyLog(type = LogType.INSERT)
    @Auth(hasPerm = AuthCodeEnum.BIZ_PUBLICITY_DQL)
    @PostMapping
    @CacheEvict(value = "yibanjia:publicity", allEntries = true)
    public Result add(@RequestBody Publicity publicity) {
        publicityService.save(publicity);
        return Result.ok();
    }
}
